﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Runtime.Serialization;

namespace Toolbox.ListExtensions
{
    public static class CloneExtension
    {
        public static U DeepClone<U>(this U source) where U : class, ISerializable
        {
            using (MemoryStream stream = new MemoryStream())
            {
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(stream, source);
                stream.Seek(0, SeekOrigin.Begin);
                return (U)formatter.Deserialize(stream);
            }
        }

        public static List<T> Clone<T>(this List<T> list) where T : ICloneable
        {
            return list.ConvertAll(x => (T)x.Clone());
        }

    }
}
